﻿Imports System.Data.SqlClient
Public Class FormCustomer
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim cn As New SqlConnection()
        cn.ConnectionString = "server=WINCTRL-B8GV7ME;uid=sa;pwd=123;database=AppSaleDb;"
        cn.Open()

        Dim cmd As New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM tbl_Customer"
        ' WHERE CustomerName LIKE N'%" & txtSearch.Text & "%' Or Phone Like N'%" & txtSearch.Text & "%' or CustomerId Like N'%" & txtSearch.Text & "%'"
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            DataGridViewCustomer.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
            ' DataGridViewCustomer.Rows.Add(reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
        End While
        reader.Close()
    End Sub

    Private Sub txtSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch.TextChanged
        DataGridViewCustomer.Rows.Clear()
        Dim cn As New SqlConnection()
        cn.ConnectionString = "server=WINCTRL-B8GV7ME;uid=sa;pwd=123;database=AppSaleDb;"
        cn.Open()

        Dim cmd As New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM tbl_Customer WHERE Name LIKE N'%" & txtSearch.Text & "%' Or Phone Like N'%" & txtSearch.Text & "%' or Address Like N'%" & txtSearch.Text & "%'"
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            DataGridViewCustomer.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
            ' DataGridViewCustomer.Rows.Add(reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
        End While
        reader.Close()
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        'FormAddEditorCus.ShowDialog()
        Dim frm As New FormAddEditorCus()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then

            DataGridViewCustomer.Rows.Clear()
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=WINCTRL-B8GV7ME;uid=sa;pwd=123;database=AppSaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "SELECT * FROM tbl_Customer WHERE Name LIKE N'%" & txtSearch.Text & "%' Or Phone Like N'%" & txtSearch.Text & "%' or Address Like N'%" & txtSearch.Text & "%'"
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                DataGridViewCustomer.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
                ' DataGridViewCustomer.Rows.Add(reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
            End While
            reader.Close()
        End If

    End Sub

    Private Sub AddEdite_Click(sender As System.Object, e As System.EventArgs) Handles AddEdite.Click
        If Me.DataGridViewCustomer.SelectedRows.Count = 0 Then
            Exit Sub
        End If


        Dim row As DataGridViewRow = Me.DataGridViewCustomer.SelectedRows(0)
        Dim frm As New FormAddEditorCus()
        frm.txtId.Text = row.Cells("ID").Value
        frm.txtName.Text = row.Cells("NameCus").Value
        frm.txtPhon.Text = row.Cells("Phone").Value
        frm.txtAddress.Text = row.Cells("Address").Value
        frm.txtId.ReadOnly = True
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then

            DataGridViewCustomer.Rows.Clear()
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=WINCTRL-B8GV7ME;uid=sa;pwd=123;database=AppSaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "SELECT * FROM tbl_Customer WHERE Name LIKE N'%" & txtSearch.Text & "%' Or Phone Like N'%" & txtSearch.Text & "%' or Address Like N'%" & txtSearch.Text & "%'"
            Dim reader As SqlDataReader = cmd.ExecuteReader()

            While reader.Read()
                DataGridViewCustomer.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
                ' DataGridViewCustomer.Rows.Add(reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
            End While
            reader.Close()
        End If
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        If Me.DataGridViewCustomer.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("តើអ្នកចង់លុបទិន្នន័យដែលឬទេ?", "លុបទិន្នន័យ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.DataGridViewCustomer.SelectedRows(0)
        Dim Code = row.Cells("ID").Value

        Dim cn As New SqlConnection()
        cn.ConnectionString = "server=WINCTRL-B8GV7ME;uid=sa;pwd=123;database=AppSaleDb;"
        cn.Open()

        Dim cmd As New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "DELETE FROM tbl_Customer WHERE ID =" & Code
        cmd.ExecuteNonQuery()

        '============Refrace Data =================
        DataGridViewCustomer.Rows.Clear()
        'Dim cn As New SqlConnection()
        'cn.ConnectionString = "server=.;uid=sa;pwd=123;database=AppSaleDb;"
        'cn.Open()

        'Dim cmd As New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM tbl_Customer WHERE Name LIKE N'%" & txtSearch.Text & "%' Or Phone Like N'%" & txtSearch.Text & "%' or Address Like N'%" & txtSearch.Text & "%'"
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            DataGridViewCustomer.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
            ' DataGridViewCustomer.Rows.Add(reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
        End While
        reader.Close()

    End Sub
End Class
